package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import db.oracle.OracleConnMgr;
import dto.C2DTO;

public class C2DAO {
	// 몇개씩 가져올건지에 따라 수정필요함
	public Vector C2list(){
		Vector v1 = new Vector();
		OracleConnMgr mgr = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String insertQuery = "select * from t_com2day";
		
		try {
			mgr = OracleConnMgr.getInstance();
			conn = mgr.getConnection();
			pstmt = conn.prepareStatement(insertQuery);
			rs = pstmt.executeQuery();
			
			while(rs.next()){
				C2DTO dto = new C2DTO();
				dto.setC2seq(rs.getInt("f_com2day_seq"));
				dto.setC2memid(rs.getString("f_member_id"));
				dto.setC2content(rs.getString("f_com2day_content"));
				dto.setC2date(rs.getString("f_com2day_date"));
				
				v1.addElement(dto);
			}		
			
		} catch (SQLException e) {
			System.out.println(e.toString());
		} catch (Exception e) {
			System.out.println(e.toString());
		} finally {
			try {
				mgr.close(conn, pstmt, rs);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return v1;
	}
	public int C2insert(C2DTO cdto){
		OracleConnMgr mgr = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		String insertQuery =
			"insert into t_com2day (f_com2day_seq, f_member_id, f_com2day_content, f_com2day_date)" +
			"values(MEMBER_SEQ.nextval,?,?, SYSTIMESTAMP)";
		int res = 0;
		
		try {
			mgr = OracleConnMgr.getInstance();
			conn = mgr.getConnection();
			pstmt = conn.prepareStatement(insertQuery);
			
			pstmt.setString(1, cdto.getC2memid());
			pstmt.setString(2, cdto.getC2content());
		
			res = pstmt.executeUpdate();			
		} catch (SQLException e) {
			System.out.println(e.toString());
		} catch (Exception e) {
			System.out.println(e.toString());
		} finally {
			try {
				mgr.close(conn, pstmt);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return res;
	}
	public int C2update(C2DTO cdto){
		OracleConnMgr mgr = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		String insertQuery =
			"UPDATE t_com2day SET f_com2day_content = ?, f_com2day_date = ?" +
			"WHERE f_com2day_seq = ?";
		int res = 0;
		
		try {
			mgr = OracleConnMgr.getInstance();
			conn = mgr.getConnection();
			pstmt = conn.prepareStatement(insertQuery);
			
			pstmt.setString(1, cdto.getC2content());
			pstmt.setString(2, cdto.getC2date());
			pstmt.setInt(3, cdto.getC2seq());
		
			res = pstmt.executeUpdate();			
		} catch (SQLException e) {
			System.out.println(e.toString());
		} catch (Exception e) {
			System.out.println(e.toString());
		} finally {
			try {
				mgr.close(conn, pstmt);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return res;
	}
	public int C2delete(int seq){
		OracleConnMgr mgr = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		String insertQuery =
			"DELETE FROM t_com2day WHERE" +
			" f_com2day_seq = ?";
		int res = 0;
		
		try {
			mgr = OracleConnMgr.getInstance();
			conn = mgr.getConnection();
			pstmt = conn.prepareStatement(insertQuery);

			pstmt.setInt(1, seq);
		
			res = pstmt.executeUpdate();			
		} catch (SQLException e) {
			System.out.println(e.toString());
		} catch (Exception e) {
			System.out.println(e.toString());
		} finally {
			try {
				mgr.close(conn, pstmt);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return res;
	}
}
